/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schtohan.restclient;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import javax.ws.rs.core.MultivaluedMap;

/**
 * Jersey REST client generated for REST resource:What Are You Doing Service
 * [friendships/create/{id}.{format}]<br>
 *  USAGE:
 * <pre>
 *        VasttrafikClient client = new VasttrafikClient();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 * </pre>
 *
 * @author Schtohan
 */
public class VasttrafikClient {
    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://api.vasttrafik.se/bin/rest.exe/";
    private static final String authKey = "71e1a3cc-0f41-403f-a378-daf4e4a44cd3";

    public VasttrafikClient() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);      
    }

        public String Location(String input) throws UniformInterfaceException 
        {
            webResource = client.resource(BASE_URI).path("location.name");
            WebResource resource = webResource;
            String[] queryParamNames = new String[]{"authKey", "input", "format"};
            String[] queryParamValues = new String[]{authKey, input, "json"};
            return resource.queryParams(getQueryOrFormParams(queryParamNames, queryParamValues)).accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
        }
        
        public String departureBoard(String id, String timeSpan) throws UniformInterfaceException 
        {
            webResource = client.resource(BASE_URI).path("departureBoard");
            WebResource resource = webResource;
            String[] queryParamNames = new String[]{"authKey", "id", "format", "timeSpan", "useBus"};
            String[] queryParamValues = new String[]{authKey, id, "json", timeSpan, "0"};
            return resource.queryParams(getQueryOrFormParams(queryParamNames, queryParamValues)).accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
        }             
        
        private MultivaluedMap getQueryOrFormParams(String[] paramNames, String[] paramValues) 
        {
            MultivaluedMap<String, String> qParams = new com.sun.jersey.api.representation.Form();
            for (int i = 0; i < paramNames.length; i++) {
                if (paramValues[i] != null) {
                    qParams.add(paramNames[i], paramValues[i]);
                }
            }
            return qParams;
        }
    
}
